package com.sxm.yiwei.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.FWContentActivity;
import com.sxm.yiwei.beans.ShopList;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.MyGridView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by KuKa on 2017/6/12.
 * 首页店铺列表
 */

public class HomeListAdapter extends BaseAdapter {
    private Context context;
    List<ShopList.DataBean> homelist;
    public HomeListOnClickListener homeListOnClickListener;


    public HomeListAdapter(Context context, List<ShopList.DataBean> homelist) {
        this.context=context;
        this.homelist=homelist;
    }

    @Override
    public int getCount() {
        return homelist.size()==0?0:homelist.size();
    }

    @Override
    public Object getItem(int position) {
        return homelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder holder;
        if (convertView == null) {
        view= LayoutInflater.from(context).inflate(R.layout.home_listitem,null);
            holder = new ViewHolder(view,position);
           /* holder.homrgrid = (MyGridView) view.findViewById(R.id.home_grid);
            holder.homelistimg = (ImageView) view.findViewById(R.id.home_listimg);
            holder.listtitle = (LinearLayout) view.findViewById(R.id.home_listtitle);
            holder.name = (TextView) view.findViewById(R.id.home_listname);
            holder.people = (TextView) view.findViewById(R.id.home_listpeople);
            holder.distance = (TextView) view.findViewById(R.id.home_listdistance);
            holder.xin = (ImageView) view.findViewById(R.id.home_listxin);
            holder.bao = (ImageView) view.findViewById(R.id.home_listbao);
            holder.zhifu = (ImageView) view.findViewById(R.id.home_listzhifu);
            holder.type = (ImageView) view.findViewById(R.id.home_listtype);
            holder.xinnum = (TextView) view.findViewById(R.id.home_listxinnum);
            view.setTag(holder);*/
        }else {
            view= convertView;
            holder= (ViewHolder) view.getTag();
        }
        /*int gradeCode = homelist.get(position).getGradeCode();
        int sellGrade = homelist.get(position).getSellGrade();

        int type = homelist.get(position).getType();
        boolean isInsurance = homelist.get(position).isIsInsurance();
        if (isInsurance==true){
            holder.bao.setImageResource(R.mipmap.baocai);
        }else {
            holder.bao.setImageResource(R.mipmap.baohui);
        }
        switch (gradeCode){
            case 0:
                holder.zhifu.setImageResource(R.mipmap.shahuise);
                break;
            case 1:
                holder.zhifu.setImageResource(R.mipmap.g10);
                break;
            case 2:
                holder.zhifu.setImageResource(R.mipmap.g1);
                break;
            case 3:
                holder.zhifu.setImageResource(R.mipmap.g2);
                break;
            case 4:
                holder.zhifu.setImageResource(R.mipmap.g3);
                break;
            case 5:
                holder.zhifu.setImageResource(R.mipmap.g4);
                break;
            case 6:
                holder.zhifu.setImageResource(R.mipmap.g6);
                break;
            case 7:
                holder.zhifu.setImageResource(R.mipmap.g5);
                break;
            case 8:
                holder.zhifu.setImageResource(R.mipmap.g7);
                break;
            case 9:
                holder.zhifu.setImageResource(R.mipmap.g8);
                break;
            case 10:
                holder.zhifu.setImageResource(R.mipmap.g9);
                break;

        }

        if (type==2){
            holder.type.setImageResource(R.mipmap.qiye);
        }else {
            holder.type.setVisibility(View.GONE);
        }
        switch (sellGrade){
            case 0:
                holder.xinnum.setText("");
                holder.xin.setVisibility(View.GONE);
                break;
            case 1:
                holder.xinnum.setText("");
                holder.xin.setImageResource(R.mipmap.xin);
                break;
            case 2:
                holder.xinnum.setText("2");
                holder.xin.setImageResource(R.mipmap.xin);
                break;
            case 3:
                holder.xinnum.setText("3");
                holder.xin.setImageResource(R.mipmap.xin);
                break;
            case 4:
                holder.xinnum.setText("4");
                holder.xin.setImageResource(R.mipmap.xin);
                break;
            case 5:
                holder.xinnum.setText("5");
                holder.xin.setImageResource(R.mipmap.xin);
                break;
            case 6:
                holder.xinnum.setText("");
                holder.xin.setImageResource(R.mipmap.zuan);
                break;
            case 7:
                holder.xinnum.setText("2");
                holder.xin.setImageResource(R.mipmap.zuan);
                break;
            case 8:
                holder.xinnum.setText("3");
                holder.xin.setImageResource(R.mipmap.zuan);
                break;
            case 9:
                holder.xinnum.setText("4");
                holder.xin.setImageResource(R.mipmap.zuan);
                break;
            case 10:
                holder.xinnum.setText("5");
                holder.xin.setImageResource(R.mipmap.zuan);
                break;
            case 11:
                holder.xinnum.setText("");
                holder.xin.setImageResource(R.mipmap.gaun);
                break;
            case 12:
                holder.xinnum.setText("2");
                holder.xin.setImageResource(R.mipmap.gaun);
                break;
            case 13:
                holder.xinnum.setText("3");
                holder.xin.setImageResource(R.mipmap.gaun);
                break;
            case 14:
                holder.xinnum.setText("4");
                holder.xin.setImageResource(R.mipmap.gaun);
                break;
            case 15:
                holder.xinnum.setText("5");
                holder.xin.setImageResource(R.mipmap.gaun);
                break;
            case 16:
                holder.xinnum.setText("");
                holder.xin.setImageResource(R.mipmap.jinguan);
                break;
            case 17:
                holder.xinnum.setText("2");
                holder.xin.setImageResource(R.mipmap.jinguan);
                break;
            case 18:
                holder.xinnum.setText("3");
                holder.xin.setImageResource(R.mipmap.jinguan);
                break;
            case 19:
                holder.xinnum.setText("4");
                holder.xin.setImageResource(R.mipmap.jinguan);
                break;
            case 20:
                holder.xinnum.setText("5");
                holder.xin.setImageResource(R.mipmap.jinguan);
                break;
        }

        double distance = homelist.get(position).getDistance();

        holder.distance.setText(distance+" 公里");
        holder.name.setText(homelist.get(position).getName());
        holder.people.setText("总成交: "+homelist.get(position).getVolume()+"单");
       Picasso.with(context).load(InternetAddressUtis.IMG_URL+homelist.get(position).getAvatar()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holder.homelistimg);*/

       /*holder.listtitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (homeListOnClickListener!=null){
                    homeListOnClickListener.homeListOnClickListener(position);
                }
            }
        });*/

        if (homelist.get(position).getServiceList()!=null){
             HomeGridViewAdapter  gridViewAdapter = new HomeGridViewAdapter(context,homelist.get(position).getServiceList());
            holder.homrgrid.setAdapter(gridViewAdapter);

            gridViewAdapter.setHomeGridItemOnClickListener(new HomeGridViewAdapter.HomeGridItemOnClickListener() {
                @Override
                public void homeGridItemOnClickListener(int i) {
                    Intent intent = new Intent(context, FWContentActivity.class);
                    String id = homelist.get(position).getServiceList().get(i).getId();
                    intent.putExtra("id",id);
                    context.startActivity(intent);
                }
            });
        }else {}
        return view;
    }

   public class ViewHolder{
        private ImageView homelistimg;
        private MyGridView homrgrid;
        private LinearLayout listtitle;
        TextView name,time,distance,people,xinnum;
        ImageView xin,bao,zhifu,type;
      // private final HomeGridViewAdapter gridViewAdapter;

       public ViewHolder(View view, final int p) {

           homrgrid = (MyGridView) view.findViewById(R.id.home_grid);
           homelistimg = (ImageView) view.findViewById(R.id.home_listimg);
           listtitle = (LinearLayout) view.findViewById(R.id.home_listtitle);
           name = (TextView) view.findViewById(R.id.home_listname);
           people = (TextView) view.findViewById(R.id.home_listpeople);
           distance = (TextView) view.findViewById(R.id.home_listdistance);
           xin = (ImageView) view.findViewById(R.id.home_listxin);
           bao = (ImageView) view.findViewById(R.id.home_listbao);
           zhifu = (ImageView) view.findViewById(R.id.home_listzhifu);
           type = (ImageView) view.findViewById(R.id.home_listtype);
           xinnum = (TextView) view.findViewById(R.id.home_listxinnum);
           view.setTag(this);
           int gradeCode = homelist.get(p).getGradeCode();
           int sellGrade = homelist.get(p).getSellGrade();
           int type1 = homelist.get(p).getType();
           boolean isInsurance = homelist.get(p).isIsInsurance();
           if (isInsurance==true){
               bao.setImageResource(R.mipmap.baocai);
           }else {
               bao.setImageResource(R.mipmap.baohui);
           }
           switch (gradeCode){
               case 0:
                   zhifu.setImageResource(R.mipmap.shahuise);
                   break;
               case 1:
                   zhifu.setImageResource(R.mipmap.g10);
                   break;
               case 2:
                   zhifu.setImageResource(R.mipmap.g1);
                   break;
               case 3:
                   zhifu.setImageResource(R.mipmap.g2);
                   break;
               case 4:
                   zhifu.setImageResource(R.mipmap.g3);
                   break;
               case 5:
                   zhifu.setImageResource(R.mipmap.g4);
                   break;
               case 6:
                   zhifu.setImageResource(R.mipmap.g6);
                   break;
               case 7:
                   zhifu.setImageResource(R.mipmap.g5);
                   break;
               case 8:
                   zhifu.setImageResource(R.mipmap.g7);
                   break;
               case 9:
                   zhifu.setImageResource(R.mipmap.g8);
                   break;
               case 10:
                   zhifu.setImageResource(R.mipmap.g9);
                   break;

           }

           if (type1==2){
               type.setImageResource(R.mipmap.qiye);
           }else {
               type.setVisibility(View.GONE);
           }
           switch (sellGrade){
               case 0:
                   xinnum.setText("");
                   xin.setVisibility(View.GONE);
                   break;
               case 1:
                   xinnum.setText("");
                   xin.setImageResource(R.mipmap.xin);
                   break;
               case 2:
                  xinnum.setText("2");
                   xin.setImageResource(R.mipmap.xin);
                   break;
               case 3:
                   xinnum.setText("3");
                   xin.setImageResource(R.mipmap.xin);
                   break;
               case 4:
                   xinnum.setText("4");
                   xin.setImageResource(R.mipmap.xin);
                   break;
               case 5:
                   xinnum.setText("5");
                   xin.setImageResource(R.mipmap.xin);
                   break;
               case 6:
                   xinnum.setText("");
                   xin.setImageResource(R.mipmap.zuan);
                   break;
               case 7:
                    xinnum.setText("2");
                   xin.setImageResource(R.mipmap.zuan);
                   break;
               case 8:
                   xinnum.setText("3");
                   xin.setImageResource(R.mipmap.zuan);
                   break;
               case 9:
                   xinnum.setText("4");
                  xin.setImageResource(R.mipmap.zuan);
                   break;
               case 10:
                   xinnum.setText("5");
                   xin.setImageResource(R.mipmap.zuan);
                   break;
               case 11:
                   xinnum.setText("");
                   xin.setImageResource(R.mipmap.gaun);
                   break;
               case 12:
                   xinnum.setText("2");
                   xin.setImageResource(R.mipmap.gaun);
                   break;
               case 13:
                   xinnum.setText("3");
                  xin.setImageResource(R.mipmap.gaun);
                   break;
               case 14:
                   xinnum.setText("4");
                   xin.setImageResource(R.mipmap.gaun);
                   break;
               case 15:
                   xinnum.setText("5");
                  xin.setImageResource(R.mipmap.gaun);
                   break;
               case 16:
                  xinnum.setText("");
                   xin.setImageResource(R.mipmap.jinguan);
                   break;
               case 17:
                   xinnum.setText("2");
                   xin.setImageResource(R.mipmap.jinguan);
                   break;
               case 18:
                   xinnum.setText("3");
                  xin.setImageResource(R.mipmap.jinguan);
                   break;
               case 19:
                   xinnum.setText("4");
                   xin.setImageResource(R.mipmap.jinguan);
                   break;
               case 20:
                   xinnum.setText("5");
                   xin.setImageResource(R.mipmap.jinguan);
                   break;
           }

           double distance1 = homelist.get(p).getDistance();
          listtitle.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (homeListOnClickListener!=null){
                       homeListOnClickListener.homeListOnClickListener(p);
                   }
               }
           });
          distance.setText(distance1+" 公里");
           name.setText(homelist.get(p).getName());
           people.setText("总成交: "+homelist.get(p).getVolume()+"单");
           Picasso.with(context).load(InternetAddressUtis.IMG_URL+homelist.get(p).getAvatar()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(homelistimg);
         /*  gridViewAdapter = new HomeGridViewAdapter(context,homelist.get(p).getServiceList());
          homrgrid.setAdapter(gridViewAdapter);

           gridViewAdapter.setHomeGridItemOnClickListener(new HomeGridViewAdapter.HomeGridItemOnClickListener() {
               @Override
               public void homeGridItemOnClickListener(int i) {

                   Intent intent = new Intent(context, FWContentActivity.class);
                   String id = homelist.get(p).getServiceList().get(i).getId();
                   intent.putExtra("id",id);
                   context.startActivity(intent);
               }
           });*/

       }
   }

    public interface HomeListOnClickListener{
        void homeListOnClickListener(int i);

    }

    public void setHomeListOnClickListener(HomeListOnClickListener homeListOnClickListener){
        this.homeListOnClickListener = homeListOnClickListener;
    }

}
